import { Shop } from "@element-plus/icons-vue";
import Mock from "mockjs";
var Random = Mock.Random;

//商品页面的数据模拟别动！！！！！！！！
var ShopData = {};
if (localStorage.getItem('user_shoplist')) {
    ShopData = JSON.parse(localStorage.getItem('user_shoplist'))
} else {
    ShopData = Mock.mock({
        "shoplist|1000": [
            {
                goodsid: '@id',
                'goodsprice|1-99999': 2,
                goodsname: '@pick(["连衣裙","电脑","手机","卫衣","秋裤","保暖衣","安踏","特步","鸿星尔克","小米","华为","连衣裙","卫衣","秋季外套","男士上衣","羽绒服","皮夹克","飞行夹克","冲锋衣","棉服","马甲","短袖","长袖T恤","睡衣","背心","太空棉服"])',
                'goodsstate|1': false,
                'goodsnum|0-9999': 1,
                goodsdetails: Random.csentence(),
                imageUrl: Random.image('200*200')
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('user_shoplist', JSON.stringify(ShopData))
}

// console.log(ShopData);
Mock.mock('/api/goods/shoplist','get', () => {
    return ShopData;
})

Mock.mock('/api/index/goods', 'post', (req) => {
    var { id } = JSON.parse(req.body);

})
//商品删除接口
Mock.mock('/api/goods/del','post', (req) => {
    console.log(req.body);
    //接收参数 , req.body 是字符串格式,需要转为json对象
    var { id } = JSON.parse(req.body);
    console.log(id)
    // //通过goodsid, 找到要删除的那一条数据, 执行删除
    var w = ShopData.shoplist.findIndex((item) => {
        return item.goodsid == id;
    })
    if (w != -1) {
        ShopData.shoplist.splice(w, 1);
        localStorage.setItem('user_shoplist', JSON.stringify(ShopData));
        console.log(ShopData.shoplist);
        return { status: 200, msg: '删除成功!', shoplist: ShopData.shoplist }
    }

})

// 商品上下架接口
Mock.mock('/api/goods/status','post', (req) => {
    console.log(req.body);
    var { id } = JSON.parse(req.body);
    // //通过goodsid, 找到要删除的那一条数据, 执行删除
    var z = ShopData.shoplist.findIndex((item) => {
        return item.goodsid == id;
    })
    if (z != -1) {
        ShopData.shoplist[z].goodsstate = !ShopData.shoplist[z].goodsstate;
        localStorage.setItem('user_shoplist', JSON.stringify(ShopData));
        console.log(ShopData.shoplist);
    }

})
//商品新增编辑接口
Mock.mock('/api/goods/add', 'post', (req) => {
    var { id } = JSON.parse( req.body ) ;
    var newshoplist = JSON.parse( req.body )
    var x = ShopData.shoplist.findIndex((item)=>{
         return item.goodsid == id; 
    })
    if (x = -1) {
        ShopData.shoplist.unshift(newshoplist)
        localStorage.setItem('user_shoplist', JSON.stringify(ShopData));
        // console.log(ShopData)
    }
})
Mock.mock('/api/goods/edit', 'post', (req) => {  
    var newshoplist = JSON.parse( req.body )
    var id = newshoplist.goodsid;
    console.log(id)
    var p = ShopData.shoplist.findIndex((item) => {
        return item.goodsid == id;
    })
    if (p != -1) {
        ShopData.shoplist.shift();
        localStorage.setItem('user_shoplist', JSON.stringify(ShopData));
        ShopData.shoplist.unshift(newshoplist);
        console.log(ShopData.shoplist);
        localStorage.setItem('user_shoplist', JSON.stringify(ShopData));
    }
})